package ar.Location.Map;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;
import ar.Location.PlaceObject.Place;
import ar.Location.TextToSpeak.TTSHelper;
import ar.Location.UI.PopUpMarkerView;
import ar.globals.Globals;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class InterestingLocations extends ItemizedOverlay<OverlayItem> {
	protected static final String TAG = "InterestingLocations";
	public static MyOverlayItem currentOverlayItem = null;
	public static Handler handlerSearchNearBy = null;

	GeoPoint center = null;
	Context ilContext = null;

	EnhancedMapView mapView = null;

	PopUpMarkerView popUpMarkerView = null;
	MapView.LayoutParams popUpMarkerLayoutParams = null;

	public InterestingLocations(EnhancedMapView mMap, Context context,
			Drawable markerIcon) {
		super(markerIcon);

		initContext(mMap, context);

		setLastFocusedIndex(-1);
		populate();

		initPopUpMarker(mapView, ilContext);
		initHandlerSearchNearBy();
	}

	public void initContext(EnhancedMapView mMap, Context context) {
		ilContext = context;
		mapView = mMap;
	}

	private void initHandlerSearchNearBy() {
		if (handlerSearchNearBy == null) {
			handlerSearchNearBy = new Handler() {
				public void handleMessage(Message msg) {
					@SuppressWarnings("unchecked")
					final ArrayList<Place> listResults = (ArrayList<Place>) msg.obj;

					if (listResults != null && listResults.size() > 0) {

						final int size = listResults.size();

						mapView.clear();

						for (int i = 0; i < size; i++) {
							Place place = listResults.get(i);
							if (place.latitude != null
									&& place.longitude != null) {
								if (place.latitude.length() > 0
										&& place.longitude.length() > 0) {
									float lat = Float
											.parseFloat(place.latitude);
									float lng = Float
											.parseFloat(place.longitude);
									int latitude = (int) (lat * 1E6);
									int longitude = (int) (lng * 1E6);
									GeoPoint point = new GeoPoint(latitude,
											longitude);
									MyOverlayItem myOverlay = new MyOverlayItem(
											point, place.name, place.address);
									myOverlay.updateInfoFrom(place);
									Globals.markers.add(myOverlay);
								}
							}
						}

						setLastFocusedIndex(-1);
						populate();

						GeoPoint myLocation = Globals.getMyLocation();
						if (myLocation != null) {
							mapView.setCenter(myLocation);
						}
					} else {
						Toast.makeText(ilContext, "Location not found...", 100)
								.show();
					}
				};
			};
		}
	}

	@Override
	public int size() {
		return Globals.markers.size();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return Globals.markers.get(i);
	}

	@Override
	protected boolean onTap(int index) {

		Log.d(TAG, "TAP TAP TAP!");

		currentOverlayItem = Globals.markers.get(index);
		showPopUpMarker(index);
		return super.onTap(index);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_UP) {
			popUpMarkerView.invisible();
		}

		return super.onTouchEvent(event, mapView);
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
	}

	public void initPopUpMarker(EnhancedMapView eMapView, Context context) {
		popUpMarkerLayoutParams = new MapView.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null,
				MapView.LayoutParams.BOTTOM_CENTER);
		popUpMarkerLayoutParams.mode = MapView.LayoutParams.MODE_MAP;

		popUpMarkerView = new PopUpMarkerView(eMapView, context);
		popUpMarkerView.setLayoutParams(popUpMarkerLayoutParams);
		popUpMarkerView.invisible();

		mapView.addView(popUpMarkerView, popUpMarkerLayoutParams);
	}

	public void showPopUpMarker(int overlayItemIndex) {
		MyOverlayItem overlayItem = Globals.markers.get(overlayItemIndex);
		if (overlayItem != null) {
			currentOverlayItem = overlayItem;

			popUpMarkerLayoutParams.point = overlayItem.getPoint();
			popUpMarkerView.geoPoint = overlayItem.getPoint();
			popUpMarkerView.updateContent(overlayItem.getTitle(),
					overlayItem.getSnippet());
			popUpMarkerView.visible();
		}
	}

	public void refreshListMarkers() {
		setLastFocusedIndex(-1);
		populate();
	}

	public void addMarker(MyOverlayItem myOverlayItem) {
		// TODO Auto-generated method stub
		Globals.markers.add(myOverlayItem);
		setLastFocusedIndex(-1);
		populate();
	}

	public void clearMarker() {
		// TODO Auto-generated method stub
		currentOverlayItem = null;

		if (!Globals.markers.isEmpty()) {

			setLastFocusedIndex(-1);
			populate();

			Globals.markers.clear();

			setLastFocusedIndex(-1);
			populate();

			popUpMarkerView.invisible();
		}
	}

	public ArrayList<MyOverlayItem> getListMarkers() {
		return (ArrayList<MyOverlayItem>) Globals.markers;
	}
}
